package com.ma.workorder;

import android.content.Intent;
import android.os.Bundle;

import com.ma.BR;
import com.ma.R;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import base.BaseListActivity;
import bean.LoginBean;
import bean.SycnRequstBean;
import database.table.Gdb;
import httpUtil.Api;
import httpUtil.Http;
import utils.ConstantUtil;
import utils.JsonUtil;
import utils.PictureUtil;
import utils.StringUtil;

public class PendingActivity extends BaseListActivity<Gdb> {
    private LoginBean loginBean;
    @Override
    protected int getLayoutId() {
        return R.layout.pending_item;
    }

    @Override
    protected int getViewModelId() {
        return BR.gdBean;
    }

    SycnRequstBean requstBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loginBean = (LoginBean) getIntent().getSerializableExtra(ConstantUtil.DATA1);
        adapter.setOnItemClickListener((view, position) -> {

            Intent intent = new Intent(this,OrderInfoActivity.class);
            intent.putExtra(ConstantUtil.DATA1,loginBean);
            intent.putExtra(ConstantUtil.DATA2,adapter.getItem(position));
            intent.putExtra(ConstantUtil.POSITION,position);

            startActivityForResult(intent,0);

        });
        requstBean = getRequstBean();


    }

    SycnRequstBean getRequstBean() {

        int page = 0;
        int xmbs = loginBean.getBMJGData().get(0).getXMBS();
        SycnRequstBean bean = new SycnRequstBean();
        bean.setGDB_SFJD(false);
        bean.setPageindex(page);
        bean.setXMBS(xmbs);
        bean.setYH_ZJ(loginBean.getID());
        return bean;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData(requstBean);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK&&requestCode==0&&data!=null){
            int p=data.getIntExtra(ConstantUtil.POSITION,-1);
            if (ConstantUtil.DEL==data.getIntExtra(ConstantUtil.ACTION,0)){
                if (p!=-1){
                    adapter.remove(p);
                }
            }else if (ConstantUtil.EDIT==data.getIntExtra(ConstantUtil.ACTION,0)){
             Gdb gdb= (Gdb) data.getSerializableExtra(ConstantUtil.DATA1);
                adapter.getData().set(p,gdb);
                adapter.notifyItemChanged(p);

            }


        }
    }

    void  getData(SycnRequstBean requstBean){
        requstBean.setPageindex(requstBean.getPageindex()+1);
        Http.post(Api.GetGDB,requstBean)
        .subscribe(res -> {
          List<Gdb>list=  JsonUtil.getList(res.getContent(),Gdb.class);
          if (!list.isEmpty()){
           addData(list);

          }
        });





    }


}
